import { defineStore } from "pinia";
import { ss } from '@/utils/storage';

const LOCAL_NAME = 'SECRET_TOKEN'

export function getToken() {
  return ss.get(LOCAL_NAME);
}

export function setToken(token: string) {
  return ss.set(LOCAL_NAME, token);
}

export function removeToken() {
  return ss.remove(LOCAL_NAME);
}

export interface AuthState {
  token: string | undefined
}

export const useAuthStore = defineStore('auth', {
  state: (): AuthState => ({
    token: getToken(),
  }),
  actions: {
    setToken(token: string) {
      this.token = token
      setToken(token)
    },
    removeToken() {
      this.token = undefined;
      removeToken();
    },
  },
});